<?php
session_start ();
$message="";
?>
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 模板</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
	charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<!--[if lt IE 9]>
         <script src="js/html5shiv.js"></script>
         <script src="js/respond.min.js"></script>
      <![endif]-->
</head>
<style>
.c {
	color: blue;
}
</style>
<body>
	<nav class="navbar navbar-default">
		<div class="container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed"
					data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
					<span class="sr-only">切换</span> <span class="icon-bar"></span> <span
						class="icon-bar"></span> <span class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href="/th4.php">点餐系统		<?php
				if (isset ( $_SESSION ['yazi'] )) {
					print_r ( "<a href='logout.php'></a>" );
				} else {
					header ( 'Refresh:2;url=/deng1.php' );
					die ( '<h3>你还未登录，请先登录</h3>' );
				}
				?></a>
			</div>
			<div class="collapse navbar-collapse"
				id="bs-example-navbar-collapse-1">
				<ul class="nav navbar-nav">
					<li class="dropdown"><a href="#" class="dropdown-toggle"
						data-toggle="dropdown">订单管理 <span class="caret"></span></a>
						<ul class="dropdown-menu">
							<li><a href="dinglie.php">订单列表</a></li>
							<li><a href="dingji.php">订单记录</a></li>
						</ul></li>
				</ul>
				<ul class="nav navbar-nav">
					<li class="dropdown"><a href="#" class="dropdown-toggle"
						data-toggle="dropdown">菜品管理 <span class="caret"></span></a>
						<ul class="dropdown-menu">
							<li><a href="jcp.php">添加菜品</a></li>
							<li><a href="cpl.php">菜品列表</a></li>
							<li class="divider"></li>
							<li><a href="jcx.php">添加菜品类型</a></li>
							<li><a href="cplie.php">菜品类型列表</a></li>
						</ul></li>
				</ul>
				<ul class="nav navbar-nav">
					<li class="dropdown"><a href="#" class="dropdown-toggle"
						data-toggle="dropdown">餐桌管理 <span class="caret"></span></a>
						<ul class="dropdown-menu">
							<li><a href="jiazhuo.php">添加餐桌</a></li>
							<li><a href="canzl.php">餐桌列表 </a></li>
						</ul></li>
				</ul>
				<ul class="nav navbar-nav">
					<li class="dropdown"><a href="#" class="dropdown-toggle"
						data-toggle="dropdown">登录管理 <span class="caret"></span></a>
						<ul class="dropdown-menu">
							<li><a href="#" data-toggle="modal" data-target="#aboutModal">关于点餐系统</a></li>
							<li><a href="xiugai.php">修改登录信息</a></li>
							<li><a href="#tui" data-toggle="modal">退出登录</a></li>
						</ul></li>
				</ul>
			</div>
		</div>
		<div class="modal fade" id="aboutModal" tabindex="-1">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal">
							<span>&times;</span><span class="sr-only">Close</span>
						</button>
						<h4 class="modal-title">关于皇子大酒店点餐系统</h4>
					</div>
					<div class="modal-body">
						本皇子大酒店，精选优质渣料，<br> 选用上等地沟油，所有汤均用高级洗脚水熬制一天一夜而成。<br>
						各位顾客可放心点餐，我们还有优质的售后服务。<br> 我们保证在顾客吃后负责挖坑，免除顾客的后顾之忧。<br>
						最后，感谢各位顾客的光临，我们的酒店因你而生辉。

					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-warning" data-dismiss="modal">确定</button>
					</div>
				</div>
			</div>
		</div>
		<div class="modal fade" id="tui" tabindex="-1">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal">
							<span>&times;</span><span class="sr-only">Close</span>
						</button>
						<h4 class="modal-title">确定退出登录</h4>
					</div>
					<div class="modal-body">你确定要退出登录吗？</div>
					<div class="modal-footer">
						<a type="button" class="btn btn-default" href="tui.php">确定</a>
						<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					</div>
				</div>
			</div>
		</div>
	</nav>
					<?php
					$e = 0;
					@$zhuoid = $_REQUEST ['zhuoid'];
					@$zhuoId = $_REQUEST ['zhuoId'];
					@$caiid = $_REQUEST ['caiId'];
					$pdo = new PDO ( 'mysql:dbname=can;charset=utf8', 'root' );
					$s = $pdo->prepare ( 'select * from zhuo_cai as room
					join zhuo
						on room.zhuoId=zhuo.id
					join cai
						on room.caiId=cai.id
					where zhuo.id=1;' );
					$s->execute ( array (
							'zhuoid' => $zhuoid 
					) );
					$a = $s->fetchAll ( PDO::FETCH_NAMED );
					// var_dump($a);
					?>
<div class="container">
		<div class="row">
			<div class="col-sm-3 col-md-2 page-left">
				<a class="btn btn-default btn-block" href="dingji.php">订单历史记录</a> <a
					class="btn btn-default btn-block" href="th4.php">返回首页</a>
			</div>
			<hr class="visible-xs-block">
				<?php
				@$zhuoId = $_REQUEST ['id'];
				
				$pdo = new PDO ( 'mysql:dbname=can;charset=utf8', 'root' );
				$s = $pdo->prepare ( 'select *,ding.id as dingId from ding join zhuo on zhuo.id=zhuoId' );
				$s->execute ();
				$b = $s->fetchAll ( PDO::FETCH_NAMED );
				// var_dump($b);
				?>
				<?php
				@$dingid = $_REQUEST ['dingId'];
				$pdo = new PDO ( 'mysql:dbname=can;charset=utf8', 'root' );
				$s = $pdo->prepare ( 'select * from ding_cai as room
					join ding
						on room.dingId=ding.id
					join cai
						on room.caiId=cai.id' );
				$s->execute ( array (
						'id' => $zhuoId 
				) );
				$Q = $s->fetchAll ( PDO::FETCH_NAMED );
// 				var_dump($Q);
				?>

	<div class="col-sm-9 col-md-10">
				<div class="panel panel-primary">
					<div class="panel-heading">
						<h1 class="panel-title">
							<span class="glyphicon glyphicon-th-list"></span> 订 单 列 表
						</h1>
					</div>
					<table class="table table-striped table-bordered table-hover">
						<tr>
							<th>订单编号</th>
							<th>餐桌号</th>
							<th>菜品总数</th>
							<th>菜品总价</th>
							<th>下单时间</th>
							<th>订单状态</th>
						</tr>
                <?php for($c=0;$c<count($b);$c++){ ?>
                <tr>
							<th><a
								href="?dingid=<?php echo $b[$c]['dingId']."&zhuoid=".$b[$c]['zhuoId']?>">mk45698-<?php echo $b[$c]['dingId']?></a></th>
							<th><?php echo $b[$c]['ming']?></th>
							<th><?php echo $b[$c]['shuliang']?></th>
							<th><?php echo $b[$c]['zongjia']?>	元</th>
							<th><?php echo $b[$c]['shijian']?></th>
							<td>
                
                <?php
				$isExist = false;
				foreach ( $Q as $item ) {
					if ($item ['dingId'] == $b [$c] ['dingId']) {
						$isExist = true;
						break;
					}
				}
				if (! $isExist) {
					?>
                未付款　<a data-toggle="modal"
								data-target="<?php echo "#quxiao-".$c;?>">取消订单</a>
                <?php
				} else {
					?>
                已付款　<a data-toggle="modal"
								data-target="<?php echo "#quxiao".$c;?>">删除订单</a>
                <?php
				}
				?>
                <div class="modal fade" id="<?php echo 'quxiao'.$c;?>">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal">
									<span>&times;</span><span class="sr-only">Close</span>
								</button>
								<h4 class="modal-title">删除确认</h4>
							</div>
							<div class="modal-body">
								你确定要删除 <span style="color: blue">mk45698-<?php echo $b[$c]['dingId']?></span>
								订单吗？<br> <br> <span style="color: red">tips：删除此订单会同步删除历史订单，届时你将不能在历史订单中查询到此订单，请慎重删除。</span>
							</div>
							<div class="modal-footer">
								<a type="button" class="btn btn-danger"
									href="dinglie.php<?php echo '?dingdanid='.$b[$c]['dingId'];?>">确定</a>
								<button type="button" class="btn btn-default"
									data-dismiss="modal">取消</button>
							</div>
						</div>
					</div>
				</div>
				<div class="modal fade" id="<?php echo 'quxiao-'.$c;?>">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal">
									<span>&times;</span><span class="sr-only">Close</span>
								</button>
								<h4 class="modal-title">取消确认</h4>
							</div>
							<div class="modal-body">
								<span style="color: red">tips：此订单未付款</span><br>
								<br>你确定要现在取消 <span style="color: blue">mk45698-<?php echo $b[$c]['dingId']?></span>
								订单吗？
							</div>
							<div class="modal-footer">
								<a type="button" class="btn btn-danger"
									href="dinglie.php<?php echo '?dinglieid='.$b[$c]['dingId'];?>">确定</a>
								<button type="button" class="btn btn-default"
									data-dismiss="modal">取消</button>
							</div>
						</div>
					</div>
				</div>
							</td>

						</tr>
                
                <?php }?>
                            </table>
				</div>
				<?php
				@$dingId = $_REQUEST ['dingdanid'];
				
				$pdo = new PDO ( 'mysql:dbname=can;charset=utf8', 'root' );
				$s = $pdo->prepare ( 'delete from ding_cai where dingId=:dingdanid' );
				if ($s->execute ( array (
						'dingdanid' => $dingId 
				) )) {
					$s = $pdo->prepare ( 'delete from ding where id=:dingdanid' );
					$s->execute ( array (
							'dingdanid' => $dingId 
					) );
				}
				$f = $s->fetch ( PDO::FETCH_NAMED );
				// var_dump($f);
				?>
				<?php
				@$dingId = $_REQUEST ['dinglieid'];
				
				$pdo = new PDO ( 'mysql:dbname=can;charset=utf8', 'root' );
				$s = $pdo->prepare ( 'delete from ding where id=:dinglieid' );
				$s->execute ( array (
						'dinglieid' => $dingId 
				) );
				$f = $s->fetch ( PDO::FETCH_NAMED );
				// var_dump($f);
				?>
        <?php
								@$zhuoId = $_REQUEST ['dingid'];
								
								$s = $pdo->prepare ( 'select *,ding.id as dingid from ding join zhuo on zhuo.id=zhuoId and ding.id=:dingid' );
								$s->execute ( array (
										'dingid' => $zhuoId 
								) );
								$r = $s->fetch ( PDO::FETCH_NAMED );
								// var_dump($r);
								?>
		
		<?php
		$isExist = false;
		foreach ( $Q as $item ) {
			if ($item ['dingId'] == $r ['dingid']) {
				$isExist = true;
				break;
			}
		}
		if (! $isExist) {
			$e = 0;
			@$id = $_REQUEST ['zhuoid'];
			@$caiid = $_REQUEST ['caiId'];
			
			$s = $pdo->prepare ( 'select * from zhuo_cai as room
						join zhuo
							on room.zhuoId=zhuo.id
						join cai
							on room.caiId=cai.id
						where zhuoId=:zhuoid;' );
			$s->execute ( array (
					'zhuoid' => $id 
			) );
			$v = $s->fetchAll ( PDO::FETCH_NAMED );
			// var_dump($v);
		} else {
			$e = 0;
			@$dingid = $_REQUEST ['dingid'];
			@$id = $_REQUEST ['zhuoid'];
			
			$s = $pdo->prepare ( 'select * from ding_cai as room
					join ding
						on room.dingId=ding.id
					join cai
						on room.caiId=cai.id
					join zhuo
						on room.zhuoId=zhuo.id
					where dingId=:dingid;' );
			$s->execute ( array (
					'dingid' => $dingid 
			) );
			$v = $s->fetchAll ( PDO::FETCH_NAMED );
			// var_dump($v);
		}
		?>
		<?php 
					
		$s=$pdo->prepare('select * from yu');
		$s->execute(array());
		$y=$s->fetch(PDO::FETCH_NAMED);
// 		var_dump($y);
		?>
		<?php
		@$caiId = $_REQUEST ['caiid'];
		@$dingId = $_REQUEST ['dingid'];
		@$jian = $r ['shijian'];
		@$mi=$_REQUEST['mi'];
		
		for ($a=0;$a<count($v);$a++){
			$e+=$v[$a]['price'];
		}
		$yu=$y['yu']-$e;
// 		var_dump($yu);
		
		// var_dump($id);
		// var_dump($id);
		// var_dump($jian);
		// var_dump($n);
// 		var_dump($e);
		// var_dump($dingId);
		date_default_timezone_set ( 'prc' );
		@$confirm = $_REQUEST ['confirm'];
		
		$params = "?dingid=" . $dingId . '&zhuoid=' . $id . '&confirm=ok';
		if (! empty ( $confirm )) {
			if ($mi!=$y['fu']){
				$message="支付密码不正确，请确认后重新输入。";
			}else {
				if ($e>$y['yu']){
					$message="余额不足，请充值。";
				}else {
					$s = $pdo->prepare ( 'insert into ding_cai values(null,:dingid,:caiId,:zhuoid)' );
					
					foreach ( $v as $item ) {
						if ($s->execute ( array ('dingid' => $dingId,'caiId' => $item ['caiId'],'zhuoid' => $id) )) {
							
						}
					}
					$time = date ( 'Y-m-d H:i:s' );
					echo "$time";
					$s = $pdo->prepare ( 'update ding set fu=:fu where id=:dingid' );
					if ($s->execute ( array ('fu' => $time,'dingid' => $dingId) )) {
							$s = $pdo->prepare ( 'delete from zhuo_cai where zhuoId=:zhuoid' );
							if ($s->execute ( array ('zhuoid' => $id) )){
								$s = $pdo->prepare ( 'update yu set yu=:yu where id=1' );
								if ($s->execute ( array ('yu' => $yu))){
									$time=date('Y-m-d H:i:s');
									$s=$pdo->prepare('insert into jilu values(null,:jian,"-",:jin,3)');
									$s->execute(array('jian'=>$time,'jin'=>$e));
									?>
									<script type="text/javascript">
									location.href="/dingji.php";
	 								</script>
									<?php
								}
							}
								}
							}
						}
					}
					?>
        <?php if (! empty ( $message )) { ?>
					<div class="alert alert-danger"><?php echo $message;?> <a
						class="close" data-dismiss="alert">&times;</a>
				</div>
					<?php } ?>
        <div class="panel panel-primary">
					<div class="panel-heading">
            <?php if (empty($zhuoId)) {?>
                <h1 class="panel-title">
							<span class="glyphicon glyphicon-th-list"></span> 点 选 "订单编号" 以 查
							看 订 单 详 情
						</h1>
                <?php }else {?>
                <h1 class="panel-title">
							<span class="glyphicon glyphicon-th-list"></span> 订 单 <span
								style="color: red"> mk45698-<?php echo $r['dingid']?> </span> 的
							详 情 如 下
						</h1>

					</div>
					<table class="table table-striped table-bordered table-hover">
						<tr>
							<th>菜品图片</th>
							<th>菜名</th>
							<th>单价</th>
							<th>数量</th>
							<th>所属餐桌</th>
						</tr>
						<?php for($c=0;$c<count($v);$c++){ ?>
						<tr>
							<th><img alt="<?php echo $v[$c]['name']?>" style="height: 40px;"
								src="<?php echo $v[$c]['src']?>"></th>
							<th><?php echo $v[$c]['name']?></th>
							<th><span class="c"><?php echo $v[$c]['price']?>元</span></th>
							<th>x1</th>
							<th><?php echo $v[$c]['ming']?></th>

						</tr>
						<?php }?>
						<?php $n=$c;?>
						</table>
					<hr>
						
						<div class="content">
						<div class="modal-body">
							<div class="row">
								<div class="caption text-right">
									所属餐桌在 <span style="color: blue"><?php echo $id; ?></span> 楼
								</div>
								<hr>
								<div class="caption text-right">
									总价是：<span style="color: blue"><?php echo $e;?></span> 元
								</div>
								<hr>
								<div class="caption text-right">
									下单时间：<span style="color: blue"><?php echo $r['shijian']?></span>
								</div>
							</div>
						</div>

						<div class="modal-footer">
							<?php
								$isExist = false;
								foreach ( $Q as $item ) {
									if ($item ['dingId'] == $r ['dingid']) {
										$isExist = true;
										break;
									}
								}
								if (! $isExist) {
									?>
				            <a data-toggle="modal" data-target="#fukuan" class="btn btn-primary">付款</a>
							<button type="button" class="btn btn-danger" data-dismiss="modal">取消</button>
					            <?php }else {?>
					            <a class="btn btn-success">此订单已付款</a>
					            <?php }?>
						</div>
					</div>
				</div>
            <?php }?>
                   <div class="modal fade" id="fukuan">
					<div class="modal-dialog">
						<div class="modal-content">
						<form action="dinglie.php<?php echo $params;?>" method="post" class="form-horizontal" >
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal">
									<span>&times;</span><span class="sr-only">Close</span>
								</button>
								<h4 class="modal-title">付款确认</h4>
							</div>
							<div class="modal-body">
							<div class="form-group">
								<label class="control-label col-sm-1 hidden-xs"></label>
								<div class="col-sm-11">
								<h5>此订单总价为：<span style="color: blue"><?php echo $e;?> 元</span></h5>
								<h5>你需要付款：<span style="color: blue"><?php echo $e;?> 元</span></h5>
								<?php if ($e>$y['yu']){?>
								<h5>可用余额为：<span style="color: blue"><?php echo $y['yu']?> 元</span>　　
								<span style="color: red">tips:可用余额不足以支付此订单，请充值。</span></h5>
								<?php }else {?>
								<h5>可用余额为：<span style="color: blue"><?php echo $y['yu']?> 元</span></h5>
								<?php }?>
								</div>
								</div>
							<div class="form-group">
			                        <label class="control-label col-sm-3 hidden-xs">支付密码：</label>
			                        <div class="col-sm-9">
			                            <input name="mi" value="" type="password" class="form-control" placeholder="请输入支付密码">
			                        </div>
			                </div>
			                </div>
							<div class="modal-footer">
								<button type="submit" class="btn btn-danger">确认付款</button>
								<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
							</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>